package com.weblog.weblog.common.utils;

import com.weblog.weblog.common.exception.BaseExceptionInterface;
import com.weblog.weblog.common.exception.BizException;
import lombok.Data;

import java.io.Serializable;

/**
 * @author gc
 * @class_name Response
 * @date 2023年09月13日 19:22
 */

@Data
public class Response<T> implements Serializable {

    private boolean success = true;

    private String message;

    private String errorCode;

    private T data;

    /*
     * 成功显示
     * @author gc
     * @date 2023/9/14 18:46
     * @param null
     * @return null
     */

    public static <T> Response<T> success(){
        return new Response<>();
    }


    public static <T> Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }

    /*
     * 错误显示
     * @author gc
     * @date 2023/9/14 18:46
     * @param null
     * @return null
     */

    public static <T> Response<T> fail(String errorMessage){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }

    public static <T> Response<T> fail(String errorCode,String errorMessage){
        Response<T> response = new Response<>();
        response.setErrorCode(errorCode);
        response.setMessage(errorMessage);
        response.setSuccess(false);
        return response;
    }


    public static <T> Response<T> fail(BizException bizException){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(baseExceptionInterface.getErrorMessage());
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        return response;
    }
}
